//
//  DHCart_PurchasedItemCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCart_PurchasedItemCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHCart_PurchasedItemCell()

@property (strong, nonatomic) NSDictionary *itemDic;

@end

@implementation DHCart_PurchasedItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.selectedButton bk_addEventHandler:^(id sender) {
        if (self.itemSelected) {
            [self.selectedButton setImage:[UIImage imageNamed:@"cart_unselected.png"] forState:UIControlStateNormal];
            self.itemSelected = NO;
        }else{
            [self.selectedButton setImage:[UIImage imageNamed:@"cart_selected.png"] forState:UIControlStateNormal];
            self.itemSelected = YES;
        }
        [self routerEventWithName:@"purchased_selected" userInfo:@{@"item":self.itemDic,@"selected":@(self.itemSelected)}];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.coverImageView bk_whenTapped:^{
        DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] init];
        vc.productID = [self.itemDic objectForKey:@"product_id"];
        vc.brandID = [self.itemDic objectForKey:@"brand_id"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }];
}

- (NSDictionary *)itemDic{
    if (!_itemDic) {
        _itemDic = [NSDictionary dictionary];
    }
    return _itemDic;
}

- (void)updateCell:(NSDictionary *)dic{
    self.itemDic = dic;
    
    self.numberLabel.text = [NSString stringWithFormat:@"商品编号:%@",[self.itemDic objectForKey:@"serial_number"]? : @""];
    [self.coverImageView dh_setImageURLString:[self.itemDic objectForKey:@"image"]];
    self.nameLabel.text = [self.itemDic objectForKey:@"name"];
    self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[[self.itemDic objectForKey:@"lease_price"] floatValue]];

    self.sizeLabel.text = [NSString stringWithFormat:@"尺码:%@",[self.itemDic objectForKey:@"size"]];

    self.countLabel.text = [NSString stringWithFormat:@"%@",[self.itemDic objectForKey:@"count"]];
    
    NSInteger count = [[dic objectForKey:@"count"] integerValue];
    self.rightLabel.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"sales_price"] floatValue] * count];
    
//    self.lineViewWidthLayout.constant = [self.leftPriceLabel.text widthForFont:[UIFont systemFontOfSize:15]];
    
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.leftPriceLabel.text attributes:attribtDic];
    self.leftPriceLabel.attributedText = attribtStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)jianAction:(id)sender {
    NSInteger count = [self.countLabel.text integerValue] - 1;
    if (count <= 0) {
        [UIAlertView bk_showAlertViewWithTitle:@"提示" message:@"是否从购物车中删除该商品?" cancelButtonTitle:@"否" otherButtonTitles:@[@"是"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                [self routerEventWithName:@"purchased_delete" userInfo:@{@"item":self.itemDic,@"selected":@(self.itemSelected)}];
            }
        }];
    }else{
        [[DHNetAPIClient shareDHNetAPIClient] request_user_cart_modify_countWithParams:@{@"cart_id":[self.itemDic objectForKey:@"cart_id"],@"count":@(count)} andBlock:^(id data, NSError *error) {
            [self routerEventWithName:@"change_count" userInfo:nil];
        }];
    }
}

- (IBAction)jiaAction:(id)sender {
    NSInteger count = [self.countLabel.text integerValue] + 1;
    [[DHNetAPIClient shareDHNetAPIClient] request_user_cart_modify_countWithParams:@{@"cart_id":[self.itemDic objectForKey:@"cart_id"],@"count":@(count)} andBlock:^(id data, NSError *error) {
        [self routerEventWithName:@"change_count" userInfo:nil];
    }];
}

@end
